/* $Header: mv_cmds.c,v 2.2 01/10/02 10:29:20 przybyls Exp $ */

/***********************************************************************/
/*                  Copyright Hewlett-Packard Co. 1993                 */
/***********************************************************************/
/* MODULE NAME: mv_cmds.c                                              */
/* ============                                                        */
/*                                                                     */
/* Author:  John DeGood                                                */
/*                                                                     */
/* MODULE PURPOSE: This module will handle the special commands sent   */
/*            to Maverick when in Controlled_By_Host mode.             */
/*                                                                     */
/***********************************************************************/

/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <error.h>
#include <method.h>
#include <mio.h>
#include <cmd_set.h>
#include <hostutil.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/

/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/

MV_FLAG_TYPE MVPending;

/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/

/*****************************/
/*   LOCAL DEFINES           */
/*****************************/

/*****************************/
/*   STATIC VARIABLES        */
/*****************************/

static MV_FLAG_TYPE MVQueue;
static INT32 ccomp_sig;

/*****************************/
/*   MACROS                  */
/*****************************/

/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/


/***********************************************************************/
/* FUNCTION: DoMVPL                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode to tell Maverick to      */
/*              start plotting GC data.                                */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVPL(void)
{
        MVQueue.bit.MVPL = 1;
}

/***********************************************************************/
/* END FUNCTION: DoMVPL                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVKC                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode when the start column    */
/*              comp key is pressed.                                   */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVKC(INT32 ccomp)
{
        ccomp_sig = ccomp;
        MVQueue.bit.MVKC = 1;
}

/***********************************************************************/
/* END FUNCTION: DoMVKC                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVKP                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode when the prepare key     */
/*              is pressed.                                            */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVKP()
{
        MVQueue.bit.MVKP = 1;
}

/***********************************************************************/
/* END FUNCTION: DoMVKP                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVKS                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode when the stop key is     */
/*              pressed.                                               */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVKS()
{
        MVQueue.bit.MVKS = 1;
}

/***********************************************************************/
/* END FUNCTION: DoMVKS                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVEN                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode when the normal end of   */
/*              an oven program occurs.                                */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVEN()
{
        /*
        ** Since rampgen.c calls StopRun(EndOvenRamp) at a 50 Hz rate
        ** until the Run_State changes, we need to protect Maverick
        ** from a 50 Hz stream of MVEN commands.  We use the
        ** MVPending flag to only send this command once.
        */
        if (!MVPending.bit.MVEN)
        {
                MVQueue.bit.MVEN = 1;
                MVPending.bit.MVEN = 1;
        }
}

/***********************************************************************/
/* END FUNCTION: DoMVEN                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVPR                                                    */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode when the normal end of   */
/*              post run occurs.                                       */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVPR()
{
        /*
        ** Since rampgen.c calls StopRun(EndPostTime) at a 50 Hz rate
        ** until the Run_State changes, we need to protect Maverick
        ** from a 50 Hz stream of MVPR commands.  We use the
        ** MVPending flag to only send this command once.
        */
        if (!MVPending.bit.MVPR)
        {
                MVQueue.bit.MVPR = 1;
                MVPending.bit.MVPR = 1;
        }
}

/***********************************************************************/
/* END FUNCTION: DoMVPR                                                */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: DoMVStartRun                                              */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Issued in Controlled_By_Host mode if the MioStartRequest() */
/*              call in StartRun() fails.  This sets a flag so we can  */
/*              keep trying to start the run at a 50 Hz rate.  In      */
/*              theory the MioStartRequest() call in StartRun() should */
/*              never fail, so this function should never be called!   */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void DoMVStartRun()
{
        MVQueue.bit.StartRun = 1;
}

/***********************************************************************/
/* END FUNCTION: DoMVStartRun                                          */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: Init_MV_Cmds                                              */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Initialize at startup the MVxx commands.                   */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void Init_MV_Cmds()
{
        MVQueue.word = 0;
        MVPending.word = 0;
}

/***********************************************************************/
/* END FUNCTION: Init_MV_Cmds                                          */
/***********************************************************************/


/***********************************************************************/
/* FUNCTION: MV_Cmds                                                   */
/* ========                                                            */
/*                                                                     */
/* PURPOSE: Run periodically from a task, this function queues         */
/*          MV cmds generated in Controlled_By_Host mode to the        */
/*          Maverick MIO card.                                         */
/*                                                                     */
/* RETURNED VALUE: none                                                */
/*                                                                     */
/***********************************************************************/

void MV_Cmds()
{
        U_INT16 parm_len;
        BIT8 *p_out_buf;
        MIO_HDR_TYPE *p_out_hdr;

        if ( MVQueue.word && (p_out_hdr = AcquirePutBuf(&p_out_buf)) )
        {
                /* format a *single* MV command */

                if (MVQueue.bit.MVPL)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXPL", p_out_buf);
                        MVQueue.bit.MVPL = 0;
                }
                else if (MVQueue.bit.MVKC)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXKC ", p_out_buf);
                        parm_len += PutIntParam(ccomp_sig, p_out_buf+parm_len, 0);
                        MVQueue.bit.MVKC = 0;
                }
                else if (MVQueue.bit.MVKP)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXKP", p_out_buf);
                        MVQueue.bit.MVKP = 0;
                }
                else if (MVQueue.bit.MVKS)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXKS", p_out_buf);
                        MVQueue.bit.MVKS = 0;
                }
                else if (MVQueue.bit.MVEN)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXEN", p_out_buf);
                        MVQueue.bit.MVEN = 0;
                }
                else if (MVQueue.bit.MVPR)
                {
                        parm_len = PutAsciiParam((BIT8 *)"MVXXPR", p_out_buf);
                        MVQueue.bit.MVPR = 0;
                }
                else
                {
                        /*
                        ** INTERNAL ERROR: THIS SHOULD NEVER OCCUR!
                        ** get rid of the MIO buffer by sending a null message
                        */
                        parm_len = 0;
                }

                /* finally, output the message buffer */
                PutMIOMsg(p_out_hdr, parm_len);
        }
}

/***********************************************************************/
/* END FUNCTION: MV_Cmds                                               */
/***********************************************************************/
